package com.lbh;

import com.jogamp.nativewindow.GraphicsConfigurationFactory;
import com.jogamp.nativewindow.VisualIDHolder;
import com.jogamp.nativewindow.awt.AWTGraphicsConfiguration;
import com.jogamp.nativewindow.awt.AWTGraphicsDevice;
import com.jogamp.nativewindow.awt.AWTGraphicsScreen;
import com.jogamp.openal.AL;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.lbh.audio.OpenAL;
import com.lbh.graphics.RenderCanvas;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

/***
 * project:JRuntime
 * package:com.lbh
 *
 * @author user:lenovo QQ:1601078366
 * @date 2021/10/17 23:55
 * @version V1.0
 */
@SuppressWarnings({"AlibabaAvoidCommentBehindStatement", "unused", "AlibabaLowerCamelCaseVariableNaming", "AlibabaSwitchStatement", "Convert2Lambda", "AlibabaConstantFieldShouldBeUpperCase"})
public class Start {

    private static final Object lock = new Object();

    public static void main(){
        OpenAL.loadAudioFiles(new String[]{"audio.wav"});
        OpenAL.audioPlay(0, AL.AL_TRUE);

        JFrame frame = new JFrame("OpenGL Context Test");

        GLProfile glp = GLProfile.getDefault();

        AWTGraphicsScreen screen = (AWTGraphicsScreen)AWTGraphicsScreen.createDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        AWTGraphicsConfiguration config = (AWTGraphicsConfiguration)
                GraphicsConfigurationFactory.getFactory(AWTGraphicsDevice.class, caps.getClass()).chooseGraphicsConfiguration(caps, caps, null, screen, VisualIDHolder.VID_UNDEFINED);

        final RenderCanvas dc = new RenderCanvas(config);

        frame.getContentPane().add(dc, BorderLayout.CENTER);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                synchronized (lock){
                    frame.setVisible(false);
                    lock.notify();
                }
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(512, 512);
        frame.setVisible(true);

        synchronized (lock){
            while (frame.isVisible()){
                try {
                    dc.repaint();
                    lock.wait(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        OpenAL.uninitialize();
    }
}
